﻿Namespace Statistics
    Public Class CustomerValueModel

        Private _percent As String = ""

        ''' <summary>
        ''' Returns the value connected to the specified date
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Value As Money
        ''' <summary>
        ''' CustomerID as String
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property CustomerName As String

        Public Property CustomerID As String

        Public Sub New()
            _value = New Money(0)
        End Sub

        Public Sub New(_customerID As String, _customerName As String, _value As Money)
            Value = _value
            CustomerName = _customerName
            CustomerID = _customerID
        End Sub

        Public Overrides Function Equals(obj As Object) As Boolean
            Try
                Dim item As CustomerValueModel = obj
                If CustomerID = item.CustomerID Then
                    Return True
                End If
                Return False
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public ReadOnly Property Percent As String
            Get
                Return _percent
            End Get
        End Property

        ''' <summary>
        ''' Set the percentage of the objects value based on the total value. Default stringformat is "0,00 %"
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub CalcPercent(totalvalue As Double, Optional stringformat As String = "0,00 %")
            If totalvalue > 0 Then
                Dim ouput As Double = (Value.Amount / totalvalue) * 100
                _percent = Math.Round(ouput, 2, MidpointRounding.AwayFromZero).ToString() + " %"
            Else
                _percent = ""
            End If
        End Sub
    End Class

    Public Class SortCustomerValueModelByValue
        Implements IComparer(Of CustomerValueModel)

        Public Function Compare(x As CustomerValueModel, y As CustomerValueModel) As Integer Implements System.Collections.Generic.IComparer(Of CustomerValueModel).Compare
            Return y.Value.Amount - x.Value.Amount
        End Function
    End Class

    Public Class SortCustomerValueModelByCustomer
        Implements IComparer(Of CustomerValueModel)

        Public Function Compare(x As CustomerValueModel, y As CustomerValueModel) As Integer Implements System.Collections.Generic.IComparer(Of CustomerValueModel).Compare
            Return x.CustomerName.CompareTo(y.CustomerName)
        End Function
    End Class
End Namespace


